#pragma once

#pragma warning(disable:4786)
#include <string>
#include <map>
#include "IServe.h"
#include "IKernel.h"
#include "../../Lib_Base/inc/SyncObjs.h"
#include "../../Lib_Base/inc/Thread.h"

namespace serve
{
	class CServe;
	class CMsgRouter;
	class CKernelImpl : public IKernel
	{
		// --------------- Begin of define (Singleton Pattern) ---------------
	public:
		static CKernelImpl*		GetInstance	(void);
	private:
		static CKernelImpl*		s_pKernel;

	protected:
		CKernelImpl();
		virtual ~CKernelImpl();
		// --------------- End of define (Singleton Pattern) ---------------
		
		// --------------- Implementation ---------------
	public:
		int b;
		virtual int TotalSocket(char type);
	    CMsgRouter*		m_pRouter;
		virtual USHORT	Release			(void)				{ delete this; return 0; }
		virtual void    CloseSocket(int socket)   ;
		virtual bool	Init(const char* IP, int nListenPort);
	
	protected:
		virtual bool	InitRouter		(void);
		virtual bool	InitServe		(const char* IP, int nListenPort);

		virtual bool	RunServes		();
		virtual void	DestroyServes	();

	private:

		enum STATUS		{ STATUS_NONE, STATUS_READY, STATUS_NORMAL, STATUS_ERR, };
		STATUS			m_eStatus;
		
		// Register		
	public:
		bool			RegisterServe	(IServe* pServe, HMODULE hModule=NULL);
		bool			UnRegisterServe	(CServe* pServe);

	private:
		typedef std::vector<CServe*>			SERVE_VEC;
		SERVE_VEC			m_setServe;
		typedef std::map<USHORT, CServe*>		SERVE_PORT_MAP;
		SERVE_PORT_MAP		m_setPortServe;
		
		typedef std::map<std::string, CServe*>	SERVE_NAME_MAP;
		SERVE_NAME_MAP		m_setNameServe;
	};
}
